/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import dto.LoggedUser;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import util.DBConnection;

/**
 *
 * @author Shuvo
 */
public class ChangePassword extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String newPass = request.getParameter("password2");
        HttpSession session = request.getSession();
        LoggedUser loggedUser = (LoggedUser) session.getAttribute("user");
        session.setAttribute("msg", "Error");
        try {
            Connection con = DBConnection.getConn();
            if (con != null) {
                Statement statement = con.createStatement();
                String query = "update LOGIN_DETAILS set PASSWORD='"+newPass+"' where USER_ID=" + loggedUser.getUserId();
                int i = statement.executeUpdate(query);
                if(i == 1) {
                    session.setAttribute("msg", loggedUser.getFullname());
                    loggedUser.setPassword(newPass);
                    System.out.println(((LoggedUser) session.getAttribute("user")).getPassword());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.sendRedirect(request.getContextPath() + "/Control.jsp");
    }
}
